import {messages} from "share/common";
import React, {Component} from 'react';
import {Checkbox, Form, Button, Spin, message, Alert} from 'antd'
import {connect} from 'react-redux'
import AccountSettingServer from 'containers/supplier-management/accounting-settings/account-setting-server'

const Group = Checkbox.Group;
const FormItem = Form.Item;

@Form.create()
class ConfirmDistribution extends Component {

  constructor(props) {
    super(props);
    this.saveOption = {
      companyOIDs: [],
      tenantId: props.tenant.id,
      artemisEnv: '',
    };
    this.state = {
      loading: false,
      setData: {},// 设置数据
    }
  }

  componentWillMount() {
    AccountSettingServer(this).getSettingsData({tenantId: this.props.tenant.id}).then(res => {
      this.setState({setData: res.data})
    });
    AccountSettingServer(this).getCompanies().then(res => {
      this.saveOption.companyOIDs = res.data.map(item => {
        return item.companyOID;
      })
    });
    AccountSettingServer(this).getCloudDev().then(res => {
      this.saveOption.artemisEnv = res.data.key;
    })
  }

  // 保存设置
  onSaveSetting = () => {
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        const params = Object.assign({}, values, this.saveOption);
        console.log(params);
        AccountSettingServer(this).saveSettingsData(params).then(res => {
          message.success(messages('supplier.key593')/*已保存*/);
        })
      }
    })
  }

  render() {
    const {getFieldDecorator} = this.props.form;
    const {loading, setData} = this.state;
    const formLayout = {
      wrapperCol: {span: 21},
      labelCol: {span: 3}
    }
    return (
      <div style={{marginTop: 16}}>
        <Alert type='info' message={messages('supplier.key635')/*启用后，为避免员工重复操作，请不要设置公司支付费用推送至账本*/} showIcon/>
        <br/>
        <Spin spinning={loading}>
          <Form>
            {
              false && (
                <FormItem label={messages('supplier.key594')/*分发确认*/} {...formLayout} style={{marginBottom: 0}}>
                  {
                    getFieldDecorator('dispenseEnable', {
                      initialValue: setData.dispenseEnable || false
                    })(
                      <Checkbox>{messages('common.status.enable')/*启用*/}</Checkbox>
                    )
                  }
                  <p style={{color: '#a9a9a9'}}>
                    {messages('supplier.key595')/*启用后，请不要将公司支付费用分发至账本；请自行在对账中心设置显示字段【员工确认状态】，【确认员工姓名】，【确认员工工号】，【员工拒绝理由】*/}
                  </p>
                </FormItem>
              )
            }
            <FormItem label={messages('supplier.key596')/*自动分发*/} {...formLayout} style={{marginBottom: 0}}>
              {
                getFieldDecorator('auDispenseEnable', {
                  initialValue: setData.auDispenseEnable || false,
                  valuePropName: 'checked',
                })(
                  <Checkbox>{messages('common.status.enable')/*启用*/}</Checkbox>
                )
              }
              <p style={{color: '#a9a9a9'}}>
                {messages('supplier.key636')/*启用后，公司支付数据会自动分发给员工进行确认（分发规则：酒店-预订人；机票-优先乘机人，未找到乘机人则分发给预订人）；若未找到，数据将不会自动分发，可手动选择人员进行分发*/}
              </p>
            </FormItem>
            <FormItem label={messages('supplier.key598')/*分发类型*/} {...formLayout}>
              {
                getFieldDecorator('dispenseTypes', {
                  initialValue: setData.dispenseTypes || [],
                  rules: [
                    {required: true, message:messages('supplier.key599')/*至少选一个*/}
                  ]
                })(
                  <Group>
                    <Checkbox style={{marginTop: 6}} value='air'>{messages('supplier.key443')/*机票*/}</Checkbox>
                    <Checkbox value='hotel'>{messages('supplier.key444')/*酒店*/}</Checkbox>
                    {/*<Checkbox value='train'>{messages('supplier.key474')/*火车*!/</Checkbox>*/}
                    {/*<Checkbox value='car'>{messages('supplier.key475')/*用车*!/</Checkbox>*/}
                    {/*<Checkbox value='yget'>{messages('supplier.key600')/*话费*!/</Checkbox>*/}
                    {/*<Checkbox value='dining'>{messages('supplier.key601')/*用餐*!/</Checkbox>*/}
                  </Group>
                )
              }
            </FormItem>
            <FormItem label={messages('supplier.key602')/*成本中心*/} {...formLayout}>
              {
                getFieldDecorator('dispenseCosts', {
                  initialValue: setData.dispenseCosts || [],
                  rules: [
                    {required: true, message:messages('supplier.key599')/*至少选一个*/}
                  ]
                })(
                  <Group>
                    <Checkbox style={{marginTop: 6}} value='SUPPLIER'>{messages('supplier.key637')/*分发消费商订单上的成本中心*/}</Checkbox>
                    <Checkbox value='APPROVE'>{messages('supplier.key604')/*分发订单关联审批单上的成本中心*/}</Checkbox>
                  </Group>
                )
              }
            </FormItem>
          </Form>
          <Button style={{marginLeft: '6%'}} onClick={this.onSaveSetting} type='primary'>{messages('common.save')/*保存*/}</Button>
        </Spin>
      </div>
    )
  }
}


function mapStateToProps(state) {
  return {
    tenant: state.login.tenant,
  }
}

export default connect(mapStateToProps)(ConfirmDistribution);
